package androidx.core.app;

import android.app.Service;
import android.app.job.JobInfo;
import android.app.job.JobInfo.Builder;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.app.job.JobServiceEngine;
import android.app.job.JobWorkItem;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build.VERSION;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import java.util.ArrayList;
import java.util.HashMap;

public abstract class JobIntentService extends Service {
    static final HashMap<ComponentName, g> ʻ = new HashMap();
    static final Object ॱॱ = new Object();
    final ArrayList<a> ʼ;
    g ˊ;
    boolean ˋ = false;
    boolean ˎ = false;
    c ˏ;
    e ॱ;
    boolean ᐝ = false;

    interface b {
        void ˏ();

        Intent ॱ();
    }

    final class a implements b {
        final int ˊ;
        final Intent ˎ;
        final /* synthetic */ JobIntentService ॱ;

        a(JobIntentService jobIntentService, Intent intent, int i) {
            this.ॱ = jobIntentService;
            this.ˎ = intent;
            this.ˊ = i;
        }

        public Intent ॱ() {
            return this.ˎ;
        }

        public void ˏ() {
            this.ॱ.stopSelf(this.ˊ);
        }
    }

    final class c extends AsyncTask<Void, Void, Void> {
        final /* synthetic */ JobIntentService ˊ;

        c(JobIntentService jobIntentService) {
            this.ˊ = jobIntentService;
        }

        protected /* synthetic */ Object doInBackground(Object[] objArr) {
            return ˊ((Void[]) objArr);
        }

        protected /* synthetic */ void onCancelled(Object obj) {
            ˊ((Void) obj);
        }

        protected /* synthetic */ void onPostExecute(Object obj) {
            ॱ((Void) obj);
        }

        protected Void ˊ(Void... voidArr) {
            while (true) {
                b ˎ = this.ˊ.ˎ();
                if (ˎ == null) {
                    return null;
                }
                this.ˊ.onHandleWork(ˎ.ॱ());
                ˎ.ˏ();
            }
        }

        protected void ˊ(Void voidR) {
            this.ˊ.ˏ();
        }

        protected void ॱ(Void voidR) {
            this.ˊ.ˏ();
        }
    }

    static abstract class g {
        int ˊ;
        final ComponentName ˋ;
        boolean ˏ;

        abstract void ˊ(Intent intent);

        g(ComponentName componentName) {
            this.ˋ = componentName;
        }

        void ˊ(int i) {
            if (!this.ˏ) {
                this.ˏ = true;
                this.ˊ = i;
            } else if (this.ˊ != i) {
                throw new IllegalArgumentException("Given job ID " + i + " is different than previous " + this.ˊ);
            }
        }

        public void ˋ() {
        }

        public void ॱ() {
        }

        public void ˎ() {
        }
    }

    static final class d extends g {
        private final Context ʻ;
        boolean ˎ;
        boolean ॱ;
        private final WakeLock ॱॱ;
        private final WakeLock ᐝ;

        d(Context context, ComponentName componentName) {
            super(componentName);
            this.ʻ = context.getApplicationContext();
            PowerManager powerManager = (PowerManager) context.getSystemService("power");
            this.ॱॱ = powerManager.newWakeLock(1, componentName.getClassName() + ":launch");
            this.ॱॱ.setReferenceCounted(false);
            this.ᐝ = powerManager.newWakeLock(1, componentName.getClassName() + ":run");
            this.ᐝ.setReferenceCounted(false);
        }

        void ˊ(Intent intent) {
            Intent intent2 = new Intent(intent);
            intent2.setComponent(this.ˋ);
            if (this.ʻ.startService(intent2) != null) {
                synchronized (this) {
                    if (!this.ॱ) {
                        this.ॱ = true;
                        if (!this.ˎ) {
                            this.ॱॱ.acquire(60000);
                        }
                    }
                }
            }
        }

        public void ˋ() {
            synchronized (this) {
                this.ॱ = false;
            }
        }

        public void ॱ() {
            synchronized (this) {
                if (!this.ˎ) {
                    this.ˎ = true;
                    this.ᐝ.acquire(600000);
                    this.ॱॱ.release();
                }
            }
        }

        public void ˎ() {
            synchronized (this) {
                if (this.ˎ) {
                    if (this.ॱ) {
                        this.ॱॱ.acquire(60000);
                    }
                    this.ˎ = false;
                    this.ᐝ.release();
                }
            }
        }
    }

    interface e {
        IBinder ˋ();

        b ˏ();
    }

    @RequiresApi(26)
    static final class i extends JobServiceEngine implements e {
        final JobIntentService ˊ;
        final Object ˋ = new Object();
        JobParameters ˎ;

        final class c implements b {
            final /* synthetic */ i ˊ;
            final JobWorkItem ˋ;

            c(i iVar, JobWorkItem jobWorkItem) {
                this.ˊ = iVar;
                this.ˋ = jobWorkItem;
            }

            public Intent ॱ() {
                return this.ˋ.getIntent();
            }

            public void ˏ() {
                synchronized (this.ˊ.ˋ) {
                    if (this.ˊ.ˎ != null) {
                        this.ˊ.ˎ.completeWork(this.ˋ);
                    }
                }
            }
        }

        i(JobIntentService jobIntentService) {
            super(jobIntentService);
            this.ˊ = jobIntentService;
        }

        public IBinder ˋ() {
            return getBinder();
        }

        public boolean onStartJob(JobParameters jobParameters) {
            this.ˎ = jobParameters;
            this.ˊ.ˊ(false);
            return true;
        }

        public boolean onStopJob(JobParameters jobParameters) {
            boolean ˋ = this.ˊ.ˋ();
            synchronized (this.ˋ) {
                this.ˎ = null;
            }
            return ˋ;
        }

        /* JADX WARNING: inconsistent code. */
        /* Code decompiled incorrectly, please refer to instructions dump. */
        public androidx.core.app.JobIntentService.b ˏ() {
            /*
            r3 = this;
            r0 = 0;
            r1 = r3.ˋ;
            monitor-enter(r1);
            r2 = r3.ˎ;	 Catch:{ all -> 0x0026 }
            if (r2 != 0) goto L_0x000a;
        L_0x0008:
            monitor-exit(r1);	 Catch:{ all -> 0x0026 }
        L_0x0009:
            return r0;
        L_0x000a:
            r2 = r3.ˎ;	 Catch:{ all -> 0x0026 }
            r2 = r2.dequeueWork();	 Catch:{ all -> 0x0026 }
            monitor-exit(r1);	 Catch:{ all -> 0x0026 }
            if (r2 == 0) goto L_0x0009;
        L_0x0013:
            r0 = r2.getIntent();
            r1 = r3.ˊ;
            r1 = r1.getClassLoader();
            r0.setExtrasClassLoader(r1);
            r0 = new androidx.core.app.JobIntentService$i$c;
            r0.<init>(r3, r2);
            goto L_0x0009;
        L_0x0026:
            r0 = move-exception;
            monitor-exit(r1);	 Catch:{ all -> 0x0026 }
            throw r0;
            */
            throw new UnsupportedOperationException("Method not decompiled: androidx.core.app.JobIntentService.i.ˏ():androidx.core.app.JobIntentService$b");
        }
    }

    @RequiresApi(26)
    static final class j extends g {
        private final JobInfo ˎ;
        private final JobScheduler ॱ;

        j(Context context, ComponentName componentName, int i) {
            super(componentName);
            ˊ(i);
            this.ˎ = new Builder(i, this.ˋ).setOverrideDeadline(0).build();
            this.ॱ = (JobScheduler) context.getApplicationContext().getSystemService("jobscheduler");
        }

        void ˊ(Intent intent) {
            this.ॱ.enqueue(this.ˎ, new JobWorkItem(intent));
        }
    }

    protected abstract void onHandleWork(@NonNull Intent intent);

    public JobIntentService() {
        if (VERSION.SDK_INT >= 26) {
            this.ʼ = null;
        } else {
            this.ʼ = new ArrayList();
        }
    }

    public void onCreate() {
        super.onCreate();
        if (VERSION.SDK_INT >= 26) {
            this.ॱ = new i(this);
            this.ˊ = null;
            return;
        }
        this.ॱ = null;
        this.ˊ = ˋ(this, new ComponentName(this, getClass()), false, 0);
    }

    public int onStartCommand(@Nullable Intent intent, int i, int i2) {
        if (this.ʼ == null) {
            return 2;
        }
        this.ˊ.ˋ();
        synchronized (this.ʼ) {
            ArrayList arrayList = this.ʼ;
            if (intent == null) {
                intent = new Intent();
            }
            arrayList.add(new a(this, intent, i2));
            ˊ(true);
        }
        return 3;
    }

    public IBinder onBind(@NonNull Intent intent) {
        if (this.ॱ != null) {
            return this.ॱ.ˋ();
        }
        return null;
    }

    public void onDestroy() {
        super.onDestroy();
        if (this.ʼ != null) {
            synchronized (this.ʼ) {
                this.ᐝ = true;
                this.ˊ.ˎ();
            }
        }
    }

    public static void enqueueWork(@NonNull Context context, @NonNull Class cls, int i, @NonNull Intent intent) {
        enqueueWork(context, new ComponentName(context, cls), i, intent);
    }

    public static void enqueueWork(@NonNull Context context, @NonNull ComponentName componentName, int i, @NonNull Intent intent) {
        if (intent == null) {
            throw new IllegalArgumentException("work must not be null");
        }
        synchronized (ॱॱ) {
            g ˋ = ˋ(context, componentName, true, i);
            ˋ.ˊ(i);
            ˋ.ˊ(intent);
        }
    }

    static g ˋ(Context context, ComponentName componentName, boolean z, int i) {
        g gVar = (g) ʻ.get(componentName);
        if (gVar == null) {
            if (VERSION.SDK_INT < 26) {
                gVar = new d(context, componentName);
            } else if (z) {
                gVar = new j(context, componentName, i);
            } else {
                throw new IllegalArgumentException("Can't be here without a job id");
            }
            ʻ.put(componentName, gVar);
        }
        return gVar;
    }

    public void setInterruptIfStopped(boolean z) {
        this.ˋ = z;
    }

    public boolean isStopped() {
        return this.ˎ;
    }

    public boolean onStopCurrentWork() {
        return true;
    }

    boolean ˋ() {
        if (this.ˏ != null) {
            this.ˏ.cancel(this.ˋ);
        }
        this.ˎ = true;
        return onStopCurrentWork();
    }

    void ˊ(boolean z) {
        if (this.ˏ == null) {
            this.ˏ = new c(this);
            if (this.ˊ != null && z) {
                this.ˊ.ॱ();
            }
            this.ˏ.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Void[0]);
        }
    }

    void ˏ() {
        if (this.ʼ != null) {
            synchronized (this.ʼ) {
                this.ˏ = null;
                if (this.ʼ != null && this.ʼ.size() > 0) {
                    ˊ(false);
                } else if (!this.ᐝ) {
                    this.ˊ.ˎ();
                }
            }
        }
    }

    b ˎ() {
        if (this.ॱ != null) {
            return this.ॱ.ˏ();
        }
        synchronized (this.ʼ) {
            if (this.ʼ.size() > 0) {
                b bVar = (b) this.ʼ.remove(0);
                return bVar;
            }
            return null;
        }
    }
}
